<template>
  <div>
    <el-form
      :model="form"
      ref="form"
      :rules="rules"
      label-width="80px"
      :inline="false"
      size="samll"
    >
      <el-row>
        <el-col :span="12">
          <el-form-item label="类别" prop="type">
            <el-radio v-model="form.type" :label="1">菜单</el-radio>
            <el-radio v-model="form.type" :label="2">按钮</el-radio>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item v-if="form.type == 1" label="组件路径" prop="path">
            <el-input v-model="form.path"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="所属路由" prop="pid">
        <el-cascader
          style="width:100%"
          v-model="form.pid"
          @change="choosePid"
          :options="list"
          :props="{ checkStrictly: true, label: 'name', value: 'id' }"
          clearable
        ></el-cascader>
      </el-form-item>
      <el-row>
        <el-col :span="12"
          ><el-form-item label="路由名称" prop="name">
            <el-input v-model="form.name"></el-input> </el-form-item
        ></el-col>
        <el-col :span="12">
          <el-form-item label="组件名称" prop="component">
            <el-input v-model="form.component"></el-input> </el-form-item
        ></el-col>
      </el-row>
      <el-row>
        <el-col :span="12"
          ><el-form-item label="权限标识" prop="permissionValue">
            <el-input v-model="form.permissionValue"></el-input> </el-form-item
        ></el-col>
        <el-col :span="12"
          ><el-form-item label="图标" prop="icon">
            <el-input v-model="form.icon"></el-input> </el-form-item
        ></el-col>
      </el-row>

      <el-form-item label="排序" prop="sort">
        <el-input-number v-model="form.sort"></el-input-number>
      </el-form-item>
      <el-form-item label="是否显示" prop="status">
        <el-radio v-model="form.status" :label="1">显示</el-radio>
        <el-radio v-model="form.status" :label="0">隐藏</el-radio>
      </el-form-item>
      <el-form-item style="display: flex; justify-content: flex-end">
        <el-button size="small" @click="handleClose">取消</el-button>
        <el-button size="small" type="primary" @click="onSubmit"
          >保存</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import permissionApi from "@/api/permission";
export default {
  name: "permissionSave",
  props: {
    permissionId: {
      type: String,
      default: "",
    },
    close: {
      type: Boolean,
      defalut: false,
    },
    list: {
      type: Array,
      defalut: () => {},
    },
  },

  data() {
    return {
      form: {
        id: "",
        pid: "",
        name: "",
        type: 1,
        path: "",
        component: "",
        icon: "",
        status: 1,
        sort: 0,
      },
      rules: {
        pid: [
          { required: true, message: "所属路由不能为空", trigger: "change" },
        ],
        name: [
          { required: true, message: "路由名称不能为空", trigger: "blur" },
        ],
        type: [
          { required: true, message: "路由类型不能为空", trigger: "change" },
        ],
      },
    };
  },
  watch: {
    close(newValue, oldValue) {
      this.form = {
        id: "",
        pid: "",
        name: "",
        type: 1,
        path: "",
        component: "",
        icon: "",
        status: 1,
        sort: 0,
      };
    },
    permissionId(newValue, oldValue) {
      if (this.permissionId) this.getInfo(this.permissionId);
    },
  },
  created() {
    console.log(this.list);
    if (this.permissionId) this.getInfo(this.permissionId);
  },
  methods: {
    choosePid(data) {
      if(data) this.form.pid = data[data.length-1]
    },
    // 获取数据
    async getInfo(id) {
      let res = await permissionApi.info(id);
      if (res.success) {
        this.form = res.data.data;
      } else {
        return this.$message.error(res.msg);
      }
    },
    // 保存
    onSubmit() {
      this.$refs.form.validate((v) => {
        if (this.permissionId) {
          this.handleUpdate();
        } else {
          this.handleAdd();
        }
      });
    },
    // 添加
    async handleAdd() {
      let res = await permissionApi.add(this.form);
      if (res.success) {
        this.$message.success("添加成功");
        this.$emit("done");
      } else {
        return this.$message.error(res.msg);
      }
    },
    // 修改
    async handleUpdate() {
      let res = await permissionApi.update(this.form);
      if (res.success) {
        this.$message.success("修改成功");
        this.$emit("done");
      } else {
        return this.$message.error(res.msg);
      }
    },

    handleClose() {
      this.$emit("done");
    },
  },
};
</script>

<style lang="scss" scoped>
</style>